<?php

/**
 *
 * 版权所有：恰维网络<qwadmin.qiawei.com>
 * 作    者：寒川<hanchuan@qiawei.com>
 * 日    期：2016-01-20
 * 版    本：1.0.0
 * 功能说明：用户控制器。
 *
 * */

namespace Qwadmin\Controller;

class StatsController extends ComController {
	
	/**
	* 订单统计
	**/
	public function order(){
		$prefix = C('DB_PREFIX');
		$uid = empty(I('get.uid')) ? session('uid') : intval(I('get.uid'));
		//echo $uid.'   '.session('uid');
		$member = D("member")->findUser(['uid' => $uid]);
		$login_member = $this->USER;
		if($uid != session('uid') && $login_member['level'] != 1 && $member['pid'] != session('uid')){
			die('您没有权限查看此代理');
		}
		$now_time = time()+24*3600;
		$now_date = strtotime(date('Y-m-d',$now_time));//今日时间
		$day30_date = strtotime(date('Y-m-d',$now_time-30*24*3600));//30日前时间
		$reg_date = strtotime(date('Y-m-d',$member['t']));//注册日期
		$for_num = 30; //循环多少次
		if($reg_date > $day30_date){//注册不慢30天  就使用注册日期
			$day30_date = $reg_date;
			$for_num = ($now_date - $reg_date)/(24*3600);
		}
		
		$data = [];
		$yesterday = '';
		for($i=0;$i<$for_num;$i++){
			$start = $now_date-($i+1)*(24*3600);
			$end = $now_date-$i*(24*3600);
			$ret = D('Income')->Income_count($uid,$member['level'],$member['proportion'],$start,$end);
			$ret['money'] = sprintf('%.2f', $ret['vip_money']+$ret['basic_money']);//充值总金额
			$ret['vip_num'] = sprintf('%.2f', $ret['vip_success_num']/($ret['vip_success_num']+$ret['vip_fail_num'])*100);//vip订单完成率
			$ret['basic_num'] = sprintf('%.2f', $ret['basic_success_num']/($ret['basic_success_num']+$ret['basic_fail_num'])*100);//普通订单完成率
			$ret['vip_ratio'] = sprintf('%.2f', $ret['vip_money']/$ret['vip_success_number']);//vip人均充值
			$ret['basic_ratio'] = sprintf('%.2f', $ret['basic_money']/$ret['basic_success_number']);//普通人均充值
			
			$ret['vip_money'] = sprintf('%.2f', $ret['vip_money']);
			$ret['basic_money'] = sprintf('%.2f', $ret['basic_money']);
			if($i==0){
				$now_data = $ret;
			}else{
				$data[date('Y-m-d',$start)] = $ret;
			}
			if($i==1){
				$yesterday = date('Y-m-d',$start);
			}
			if($end < $day30_date){
				break;
			}
		}
		//获取本月充值
		$now_month=strtotime(date('Y-m',$now_time-24*3600));//获取本月时间
		$next_month=strtotime(date('Y-m-d', $now_time-24*3600));//获取本月时间
		$ret = D('Income')->Income_muchday($uid,$now_month,$next_month);
		$ret['money'] = sprintf('%.2f', $ret['vip_money']+$ret['basic_money']);//充值总金额
		$ret['vip_num'] = sprintf('%.2f', $ret['vip_success_num']/($ret['vip_success_num']+$ret['vip_fail_num'])*100);//vip订单完成率
		$ret['basic_num'] = sprintf('%.2f', $ret['basic_success_num']/($ret['basic_success_num']+$ret['basic_fail_num'])*100);//普通订单完成率
		$ret['vip_ratio'] = sprintf('%.2f', $ret['vip_money']/$ret['vip_success_number']*100);//vip人均充值
		$ret['basic_ratio'] = sprintf('%.2f', $ret['basic_money']/$ret['basic_success_number']*100);//普通人均充值
		
		$ret['vip_money'] = sprintf('%.2f', $ret['vip_money']);
		$ret['basic_money'] = sprintf('%.2f', $ret['basic_money']);
		$this->assign('month_data',$ret);
		
		//获取总充值
		$now_month=strtotime(date('Y-m-d',$now_time-24*3600));//获取昨天
		$ret = D('Income')->Income_muchday($uid,0,$now_month);
		$ret['money'] = sprintf('%.2f', $ret['vip_money']+$ret['basic_money']);//充值总金额
		$ret['vip_num'] = sprintf('%.2f', $ret['vip_success_num']/($ret['vip_success_num']+$ret['vip_fail_num'])*100);//vip订单完成率
		$ret['basic_num'] = sprintf('%.2f', $ret['basic_success_num']/($ret['basic_success_num']+$ret['basic_fail_num'])*100);//普通订单完成率
		$ret['vip_ratio'] = sprintf('%.2f', $ret['vip_money']/$ret['vip_success_number']*100);//vip人均充值
		$ret['basic_ratio'] = sprintf('%.2f', $ret['basic_money']/$ret['basic_success_number']*100);//普通人均充值
		
		$ret['vip_money'] = sprintf('%.2f', $ret['vip_money']);
		$ret['basic_money'] = sprintf('%.2f', $ret['basic_money']);
		$this->assign('count_data',$ret);
		
		$this->assign('now_data',$now_data);
		$this->assign('yesterday',$yesterday);
		$this->assign('data',$data);
		$this->assign('uid',empty(I('get.uid'))?0:$uid);
		$this->display();
	}
	
	/**
	* 获取单日订单统计
	**/
	public function day_order(){
		$prefix = C('DB_PREFIX');
		$uid = empty(I('post.uid')) ? session('uid') : intval(I('post.uid'));
		$member = D("member")->findUser(['uid' => $uid]);
		$login_member = $this->USER;
		if($uid != session('uid') && $login_member['level'] != 1 && $member['pid'] != session('uid')){
			die('您没有权限查看此代理');
		}
		$now_time = time()+24*3600;
		$now_date = strtotime(date('Y-m-d',$now_time));//今日时间
		$start_date = strtotime(date('Y-m-d',$now_time-24*3600));//30日前时间
		
		$ret = D('Income')->Income_count($uid,$member['level'],$member['proportion'],$start_date,$now_time);
		$ret['money'] = sprintf('%.2f', $ret['vip_money']+$ret['basic_money']);//充值总金额
		$ret['vip_num'] = sprintf('%.2f', $ret['vip_success_num']/($ret['vip_success_num']+$ret['vip_fail_num'])*100);//vip订单完成率
		$ret['basic_num'] = sprintf('%.2f', $ret['basic_success_num']/($ret['basic_success_num']+$ret['basic_fail_num'])*100);//普通订单完成率
		$ret['vip_ratio'] = sprintf('%.2f', $ret['vip_money']/$ret['vip_success_number']);//vip人均充值
		$ret['basic_ratio'] = sprintf('%.2f', $ret['basic_money']/$ret['basic_success_number']);//普通人均充值
		
		$ret['vip_money'] = sprintf('%.2f', $ret['vip_money']);
		$ret['basic_money'] = sprintf('%.2f', $ret['basic_money']);
		
		echo json_encode($ret);
	}
	
	/**
	* 用户统计
	**/
	public function members(){
		$prefix = C('DB_PREFIX');
		$uid = empty(I('get.uid')) ? session('uid') : intval(I('get.uid'));
		//echo $uid.'   '.session('uid');
		$member = D("member")->findUser(['uid' => $uid]);
		$login_member = $this->USER;
		if($uid != session('uid') && $login_member['level'] != 1 && $member['pid'] != session('uid')){
			die('您没有权限查看此代理');
		}
		$now_time = time()+24*3600;
		$now_date = strtotime(date('Y-m-d',$now_time));//今日时间
		$day30_date = strtotime(date('Y-m-d',$now_time-30*24*3600));//30日前时间
		$reg_date = strtotime(date('Y-m-d',$member['t']));//注册日期
		$for_num = 30; //循环多少次
		if($reg_date > $day30_date){//注册不慢30天  就使用注册日期
			$day30_date = $reg_date;
			$for_num = ($now_date - $reg_date)/(24*3600);
		}
		$now_data = [];
		$data = [];
		$yesterday = '';
		for($i=0;$i<$for_num;$i++){
			$start = $now_date-($i+1)*(24*3600);
			$end = $now_date-$i*(24*3600);
			
			$ret = D('Member')->User_count($uid,$member['level'],$start,$end);
			$ret['count'] = $ret['man']+$ret['woman']+$ret['Unknown'];//新增总数
			$ret['subscribe_ratio'] = sprintf('%.2f', $ret['subscribe']/$ret['count']*100);//关注率
			$ret['pay_ratio'] = sprintf('%.2f', $ret['pay_num']/$ret['count']*100);//支付率
			if($i==0){
				$now_data = $ret;
			}else{
				$data[date('Y-m-d',$start)] = $ret;
			}
			if($i==1){
				$yesterday = date('Y-m-d',$start);
			}
		}
		//获取本月新增用户
		$now_month=strtotime(date('Y-m',$now_time-24*3600));//获取本月时间
		$next_month=strtotime(date('Y-m', strtotime('+1 month')));//获取本月时间
		$ret = D('Member')->User_count($uid,$member['level'],$now_month,$next_month);
		$ret['count'] = $ret['man']+$ret['woman']+$ret['Unknown'];//新增总数
		$ret['subscribe_ratio'] = sprintf('%.2f', $ret['subscribe']/$ret['count']*100);//关注率
		$ret['pay_ratio'] = sprintf('%.2f', $ret['pay_num']/$ret['count']*100);//支付率
		$this->assign('month_data',$ret);
		
		//获取总用户数
		$now_month=strtotime(date('Y-m',$now_time-24*3600));//获取本月时间
		$next_month=strtotime(date('Y-m', strtotime('+1 month')));//获取本月时间
		$ret = D('Member')->User_count($uid,$member['level'],0,$now_month);
		$ret['count'] = $ret['man']+$ret['woman']+$ret['Unknown'];//新增总数
		$ret['subscribe_ratio'] = sprintf('%.2f', $ret['subscribe']/$ret['count']*100);//关注率
		$ret['pay_ratio'] = sprintf('%.2f', $ret['pay_num']/$ret['count']*100);//支付率
		$this->assign('count_data',$ret);
		
		$this->assign('now_data',$now_data);
		$this->assign('yesterday',$yesterday);
		$this->assign('data',$data);
		$this->assign('uid',empty(I('get.uid'))?0:$uid);
		$this->display();
	}
	/**
	* 获取今日用户统计
	**/
	public function day_members(){
		$prefix = C('DB_PREFIX');
		$uid = empty(I('post.uid')) ? session('uid') : intval(I('post.uid'));
		$member = D("member")->findUser(['uid' => $uid]);
		$login_member = $this->USER;
		if($uid != session('uid') && $login_member['level'] != 1 && $member['pid'] != session('uid')){
			die('您没有权限查看此代理');
		}
		$now_time = time()+24*3600;
		$now_date = strtotime(date('Y-m-d',$now_time));//今日时间
		$start_date = strtotime(date('Y-m-d',$now_time-24*3600));//30日前时间
		
		$ret = D('Member')->User_count($uid,$member['level'],$start_date,$now_time);
		$ret['count'] = $ret['man']+$ret['woman']+$ret['Unknown'];//新增总数
		$ret['subscribe_ratio'] = sprintf('%.2f', $ret['subscribe']/$ret['count']*100);//关注率
		$ret['pay_ratio'] = sprintf('%.2f', $ret['pay_num']/$ret['count']*100);//支付率
		
		echo json_encode($ret);
	}
	/**
	* 导出结算表
	**/
	public function download_excel(){

        header("Content-type: text/html; charset=utf-8");
        set_time_limit(0);

        $prefix = C('DB_PREFIX');
		$uid = I("uid" , 0 , 'intval');//获取需要导出下级帐号的ID
        $user_gid=$this->USER;//获取登陆账户的权限
		
		if(empty($user_gid['level'])){
			die('未登录，不可下载表格');
		}
		if(empty($uid)){
			die('未确定上级账户，不可下载');
		}
        
		$start_date = htmlspecialchars(I("start_date" , '') , ENT_NOQUOTES);
		$end_date = htmlspecialchars(I("end_date" , '') , ENT_NOQUOTES);
		$current_timestamp   = (!empty($end_date))?$end_date:date('Y-m-d');
        $yesterday_timestamp = (!empty($start_date))?$start_date:date('Y-m-d',strtotime($current_timestamp)-(24*3600));
		
		//定义表头
		$str = "会员ID\t会员名称\t打款帐号类型\t帐号名称\t打款账号\t收入金额\t分成比例\t应付金额\t\n";
        $str = iconv('utf-8','gb2312',$str);
		
		//获取帐号的信息
        $member = M('member')->where("uid={$uid}")->find();
		//获取帐号包含其下级的总收益
		$pay_money = D('Income')->Income_count($member['uid'],$member['level'],$yesterday_timestamp,$current_timestamp);
		
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['uid']) . "\t";
		$member['user'] = str_replace("—", "-", $member['user']);
		$member['user'] = str_replace("-", "-", $member['user']);
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['user']) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['account_type']==1?'支付宝':($member['account_type']==2?'微信':'银行卡')) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['truename']) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['alipay']) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', sprintf('%.2f',$pay_money)) . "\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', $member['proportion']) . "%\t";
		$str .= iconv('utf-8', 'gbk//IGNORE', sprintf('%.2f',$pay_money*($member['proportion']/100))) . "\t\n\n";
		$str .= iconv('utf-8', 'gbk//IGNORE', '下线分成') . "\t\n";
		
		
		$filename = iconv('utf-8', 'gbk//IGNORE', $yesterday_timestamp."--".$current_timestamp."--{$member['user']}-超高转化收益数据表.xls");

		
		//获取帐号创建的所有下级
		$where = "{$prefix}member.pid={$uid}";
        $user = M('member');
        $list  = $user->field("{$prefix}member.*")
						->where($where)
						->order('uid desc')
						->select();

        foreach ($list as $key => $v) {
			$pay_money = D('Income')->Income_count($v['uid'],$v['level'],$yesterday_timestamp,$current_timestamp);
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['uid']) . "\t";
			$v['user'] = str_replace("—", "-", $v['user']);
			$v['user'] = str_replace("-", "-", $v['user']);
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['user']) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['account_type']==1?'支付宝':($v['account_type']==2?'微信':'银行卡')) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['truename']) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['alipay']) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', sprintf('%.2f',$pay_money)) . "\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', $v['proportion']) . "%\t";
			$str .= iconv('utf-8', 'gbk//IGNORE', sprintf('%.2f',$pay_money*($v['proportion']/100))) . "\t\n";
        }

        $this->exportExcel($filename,$str);
    }

	private  function exportExcel($filename,$content){
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
		header("Content-Type:application/force-download");
		header("Content-Type:application/vnd.ms-execl");
		header("Content-Type:application/octet-stream");
		header("Content-Type:application/download");
		header("Content-Disposition: attachment; filename=".$filename);
		header("Content-Transfer-Encoding:binary");
		echo $content;
	}
}
